package cc.shacocloud.luanniao.core.service.text.splitter

import cc.shacocloud.luanniao.core.model.constant.PreProcessingRuleTypeEnum
import cc.shacocloud.luanniao.core.model.constant.PreProcessingRuleTypeEnum.REMOVE_EXTRA_SPACES

/**
 * 文本内容分割服务
 *
 * @author 思追(shaco)
 */
interface TextSplitterService {

    /**
     * 清理和分割文本
     *
     * @param text 文本
     * @param preProcessingRules 文本预处理规则 [preProcessingText]
     * @param separators 分隔符
     * @param keepSeparator 是否保留文本中的分隔符
     */
    fun cleanAndSplitText(
        text: String,
        preProcessingRules: List<PreProcessingRuleTypeEnum> = listOf(REMOVE_EXTRA_SPACES),
        separators: List<String> = listOf("\n", "。", ". ", " ", ""),
        keepSeparator: Boolean = true,
    ): List<String> {
        val newText = preProcessingText(text, preProcessingRules)
        return splitText(newText, separators, keepSeparator)
    }


    /**
     * 预处理文本
     */
    fun preProcessingText(
        text: String,
        preProcessingRules: List<PreProcessingRuleTypeEnum>,
    ): String {
        var newText = text
        for (preProcessingRule in preProcessingRules) {
            newText = preProcessingRule.preProcessing(newText)
        }
        return newText
    }

    /**
     * 分割文本
     *
     * @param text 文本
     * @param separators 分隔符
     * @param keepSeparator 是否保留文本中的分隔符
     */
    fun splitText(
        text: String,
        separators: List<String> = listOf("\n", "。", ". ", " ", ""),
        keepSeparator: Boolean = true,
    ): List<String>

}